package com.mcy.springbatch.job;

import com.mcy.springbatch.entity.SysUser;
import com.mcy.springbatch.reader.ReadUserDataFlatFileItemReader;
import com.mcy.springbatch.reader.ReadUserDataJpaPagingItemReader;
import com.mcy.springbatch.writer.SysUserItemWriter;
import com.mcy.springbatch.writer.SysUserJdbcBatchItemWriter;
import com.mcy.springbatch.writer.SysUserJpaItemWriter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.launch.support.RunIdIncrementer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 对数据库进行读写数据
 *
 * 读：
 * JpaPagingItemReader
 * RepositoryItemReader
 * JdbcCursorItemReader
 * jdbcPagingItemReader
 *
 * 写：
 * JpaItemWriter
 * JdbcBatchItemWriter
 * HibernateItemWriter
 * RepositoryItemWriter
 *
 * @return
 */
@Slf4j
@Configuration
public class JReadWriteDataWithDbJobConfig extends BaseJobConfig {

    @Autowired
    private ReadUserDataJpaPagingItemReader readUserDataJpaPagingItemReader;

    //jdbc的方式写writer
    @Autowired
    private SysUserJdbcBatchItemWriter sysUserJdbcBatchItemWriter;

    //jpa的方式写writer
    @Autowired
    private SysUserJpaItemWriter sysUserJpaItemWriter;

    @Autowired
    private ReadUserDataFlatFileItemReader readUserDataFlatFileItemReader;

    @Bean
    public Job readWriteDataWithDbJob(){
        return jobBuilderFactory.get("readWriteDataWithDbJob")
                .incrementer(new RunIdIncrementer())
                .start(readerFromDBStep())
                .build();
    }

    @Bean
    public Step readerFromDBStep(){
        return stepBuilderFactory.get("readerFromDBStep")
                .<SysUser,SysUser>chunk(20)
                .reader(readUserDataFlatFileItemReader)
                .writer(sysUserJdbcBatchItemWriter)
                .build();
    }
}

